using Microsoft.SPOT;
using NetduinoHappyHour.Hardware;
using NetduinoRobot.Common;
using System;
using System.Reflection;
using System.Text;

namespace NetduinoHappyHour.Util
{
    /// <summary>
    /// Collects all update members of SystemConfig
    /// Written by: Salvador Richter
    /// Appache License Version 2.0 
    /// </summary>
    public class ReadSystemConfig : IExecutable, IInteractionResponseMessage
    {
        private string _message;

        public ReadSystemConfig() { }

        public void Execute()
        {
            _message = ReadConfig();
        }

        public string ResponseMessage()
        {
            return _message;
        }

        public string ReadConfig()
        {
            StringBuilder sBuilder = new StringBuilder();
            SystemConfig sConfig = SystemConfig.Instance();
            Type myType = sConfig.GetType();

/*
            // Fields are not able or allowed to be updated
            FieldInfo[] myFields = myType.GetFields();
            foreach (FieldInfo field in myFields)
            {
                sBuilder.Append(field.Name);
                sBuilder.Append("=");
                sBuilder.Append(field.GetValue(sConfig));
                sBuilder.Append(CommandHelper.CommandSeparator);
            }
*/

            // due members are not given by type the get_ Methods are chosen to retrieve setable members
            MethodInfo[] myMethods = myType.GetMethods();
            foreach (MethodInfo method in myMethods)
            {
                if(method.Name.Substring(0,4).Equals("get_"))
                {
                    sBuilder.Append(method.Name.Substring(4));
                    sBuilder.Append("=");
                    sBuilder.Append(method.Invoke(sConfig,null));
                    sBuilder.Append(CommandHelper.CommandSeparator);
                }
            }
            return sBuilder.ToString();
        }
    }
}
